package mainpackage;

import java.sql.SQLException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Scanner;

import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

import ui.MainWindow;

public class Main {

	// TODO no check for empty strings

	private static DataManager dataManager = new DataManager("database.db");
	private static Scanner scanner = new Scanner(System.in);

	/**
	 * @param args
	 * @throws UnsupportedLookAndFeelException 
	 * @throws IllegalAccessException 
	 * @throws InstantiationException 
	 * @throws ClassNotFoundException 
	 */
	public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, UnsupportedLookAndFeelException {
		graphicalInterface();
		//consoleInerface();
	}
	
	private static void graphicalInterface() throws ClassNotFoundException, InstantiationException, IllegalAccessException, UnsupportedLookAndFeelException {
		UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		MainWindow mw = new MainWindow();
		mw.getPresenter().setDataManager(dataManager);
		mw.setVisible(true);
	}
	
	
	

	private static void consoleInerface() {
		System.out.println("Available commands:");
		System.out.println("add/delete/edit todo");
		System.out.println("add/delete category");
		System.out.println("show all");
		System.out.println("show day schedule");
		System.out.println("quit");
		while (true) {
			String input = scanner.nextLine();
			if (input.equals("quit")) {
				return;
			} else if (input.equals("add todo")) {
				processAddTodo();
			} else if (input.equals("edit todo")) {
				processEditTodo();
			} else if (input.equals("delete todo")) {
				processDeleteTodo();
			} else if (input.equals("add category")) {
				processAddCategory();
			} else if (input.equals("delete category")) {
				processDeleteCategory();
			} else if (input.equals("show all")) {
				processShowAll();
			} else if (input.equals("show day schedule")) {
				processShowDaySchedule();
			} else {
				System.out.println("bad command");
			}
		}
	}

	private static void processShowDaySchedule() {
		Date start = getDayBeggining();
		Date end = getDayEnd();

		List<Todo> todos = dataManager.getTodo(start, end);
		for (Todo todo : todos) {
			System.out.println("id:" + todo.getId());
			System.out.println("title:" + todo.getName());
			System.out.println("description:" + todo.getDescription());
			System.out.println("start:" + todo.getStartTime());
			System.out.println("end:" + todo.getEndTime());
			System.out.println("category:" + todo.getCategory().getName());
			System.out.println("");
		}
	}

	private static Date getDayBeggining() {
		Date now = new Date();
		SimpleDateFormat df = new SimpleDateFormat("yy/MM/dd");
		String noTimeString = df.format(now);
		Date dayBeggining = null;
		try {
			dayBeggining = df.parse(noTimeString);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return dayBeggining;
	}

	private static Date getDayEnd() {
		Date now = new Date();
		SimpleDateFormat df = new SimpleDateFormat("yy/MM/dd");
		String noTimeString = df.format(now);
		String endString = noTimeString + " 23:59";
		Date dayEnd = null;
		SimpleDateFormat fullFormat = new SimpleDateFormat("yy/MM/dd H:m");
		try {
			dayEnd = fullFormat.parse(endString);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return dayEnd;
	}

	private static void processDeleteCategory() {
		System.out.println("Title:");
		String name = scanner.nextLine();
		try {
			dataManager.removeCategory(name);
		} catch (SQLException e) {
			System.out.println("Could not delete category. Category in use.");
		}
	}

	private static void processDeleteTodo() {
		System.out.println("Todo's id:");
		int id = scanner.nextInt();
		Todo todo = dataManager.getTodoById(id);
		if (todo == null) {
			System.out.println("Wrong id");
			return;
		}
		dataManager.removeTodo(todo);
		System.out.println("Todo successfully deleted");
	}

	private static void processEditTodo() {
		System.out.println("Todo's id:");
		int id = scanner.nextInt();
		Todo todo = dataManager.getTodoById(id);
		if (todo == null) {
			System.out.println("Wrong id");
			return;
		}
		System.out.println("Todo's title:");
		String name = scanner.nextLine();
		System.out.println("Todo's description:");
		String description = scanner.nextLine();
		System.out.println("Todo's start time as \"yy/MM/dd H:m\":");
		String start = scanner.nextLine();
		System.out.println("Todo's end time as \"yy/MM/dd H:m\":");
		String end = scanner.nextLine();
		System.out
				.println("Todo's category (category must be already created):");
		String categoryString = scanner.nextLine();
		Category category = dataManager.getCategoryByName(categoryString);
		if (category == null) {
			System.out.println("Category not found");
			return;
		}
		try {
			dataManager.editTodo(todo, name, description, start, end, category);
			System.out.println("Todo successfully edited");
		} catch (EndDateIsBeforeStartException e) {
			System.out.println("Start time must be before end time");
			return;
		} catch (ParseException e) {
			System.out.println("Cannot parse date");
			return;
		}

	}

	private static void processAddCategory() {
		System.out.println("Title:");
		String name = scanner.nextLine();
		try {
			dataManager.addCategory(name);
			System.out.println("Category successfully added");
		} catch (NoColorsLeftException e) {
			System.out.println("Too many categories");
			return;
		} catch (CategoryNameNotUniqueException e) {
			System.out.println("Category with this name already exists");
			return;
		}
	}

	private static void processShowAll() {
		List<Todo> todos = dataManager.getAllTodos();
		for (Todo todo : todos) {
			System.out.println("id:" + todo.getId());
			System.out.println("title:" + todo.getName());
			System.out.println("description:" + todo.getDescription());
			System.out.println("start:" + todo.getStartTime());
			System.out.println("end:" + todo.getEndTime());
			System.out.println("category:" + todo.getCategory().getName());
			System.out.println("");
		}
	}

	private static void processAddTodo() {
		System.out.println("Todo's title:");
		String name = scanner.nextLine();
		System.out.println("Todo's description:");
		String description = scanner.nextLine();
		System.out.println("Todo's start time as \"yy/MM/dd H:m\":");
		String start = scanner.nextLine();
		System.out.println("Todo's end time as \"yy/MM/dd H:m\":");
		String end = scanner.nextLine();
		System.out
				.println("Todo's category (category must be already created):");
		String categoryString = scanner.nextLine();
		Category category = dataManager.getCategoryByName(categoryString);
		if (category == null) {
			System.out.println("Category not found");
			return;
		}
		Todo todo;
		try {
			todo = new Todo(name, description, start, end, category);
			dataManager.addTodo(todo);
			System.out.println("Todo successfully added");
			return;
		} catch (EndDateIsBeforeStartException e) {
			System.out.println("Start time must be before end time");
			return;
		} catch (ParseException e) {
			System.out.println("Cannot parse date");
			return;
		}

	}
	

}
